package io.renren.modules.courseSmall.controller;

import io.renren.common.annotation.Login;
import io.renren.common.config.ListPageUtils;
import io.renren.common.utils.R;
import io.renren.modules.courseSmall.entity.CourseSmall;
import io.renren.modules.courseSmall.entity.CourseSmallAndTeacherEntity;
import io.renren.modules.courseSmall.entity.CourseSmallClassification;
import io.renren.modules.courseSmall.service.courseSmallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/courseSmall")
public class courseSmallController {
        @Autowired
    courseSmallService courseSmallService;
    /**
     * 查询课程信息
     */
    @PostMapping("getCourseInfoById")
    public R getCourseInfoById(@RequestBody Map map){
        CourseSmallAndTeacherEntity courseSmallAndTeacherEntity =  courseSmallService.getCourseInfoById(map);

        //增加一次播放
        courseSmallService.addSeeCount(map);
         return R.ok("查看成功",100).put("data",courseSmallAndTeacherEntity);
    }
    public R getCourseById(Map map){
        CourseSmall courseSmall = courseSmallService.getCourseById(map);
        return R.ok();
    }

    /**
     * 获取全部微课程
     */
    @PostMapping("getCourseSmall")
    public R getCourseSmall(@RequestBody Map map){
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        List courseSmallList = courseSmallService.getCourseSmall(map);
        if(courseSmallList.size()>0){
            ListPageUtils pageUtil = new ListPageUtils(courseSmallList,pageSize);
            List list = pageUtil.getPagedList(pageNum);
            if(list.size() > 0){
                return R.ok("获取全部微课程",100).put("data",list).put("nextNum",pageNum+1);
            }
            return R.ok("获取全部微课程",100).put("data",list).put("nextNum",pageNum+1);
        }
        return R.ok("获取全部微课程",100).put("data",courseSmallList).put("nextNum",pageNum+1);
    }

    /**
     * 随机推荐微课程  换一换功能
     * 因为是确定了要一次提供几次数据 所以说不需要进行分页
     */
    @GetMapping("getRandomCourseSmall")
    public R getRandomCourseSmall(){
        List list = courseSmallService.getRandomCourseSmall();
        return R.ok().put("data",list);
    }

    /**
     * 添加收藏
     */
    @PostMapping("addCourseSmallCollection")
    @Login
    public R addCourseSmallCollection(@RequestBody Map map){
        courseSmallService.addCourseSmallCollection(map);
        return R.ok("添加收藏成功",100);
    }

    /**
     * 取消收藏
     */
    @PostMapping("delCourseSmallCollection")
    @Login
    public R delCourseSmallCollection(@RequestBody Map map){
        courseSmallService.delCourseSmallCollection(map);
        return R.ok("取消收藏成功",100);
    }

    /**
     * 查看收藏课程
     */
    @PostMapping("getCourseSmallCollection")
    @Login
    public R getCourseSmallCollection(@RequestBody Map map){
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        List courseSmallCollectionList = courseSmallService.getCourseSmallCollection(map);
        if(courseSmallCollectionList.size()>0){
            ListPageUtils pageUtil = new ListPageUtils(courseSmallCollectionList,pageSize);
            List list = pageUtil.getPagedList(pageNum);
            if(list.size() > 0){
                return R.ok("获取收藏微课程成功",100).put("data",list).put("nextNum",pageNum+1);
            }
            return R.ok("获取收藏微课程成功",100).put("data",list).put("nextNum",-1);
        }
        return R.ok("您还没有收藏微课程呢哦",100).put("data",courseSmallCollectionList).put("nextNum",-1);
    }


    /**
     * 递归获取微课分类
     */
    @GetMapping("getCourseSmallClassification")
    public R getCourseSmallClassification(@RequestParam int id){
        List<CourseSmallClassification> courseSmallClassificationList =  courseSmallService.getCourseSmallClassification(id);

        return R.ok("",100).put("data",courseSmallClassificationList);
    }

    /**
     * 根据分类获取微课程
     */
    @PostMapping("getCourseSmallByClassification")
    public  R getCourseSmallByClassification(@RequestBody Map map){
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        int id = Integer.parseInt(map.get("classificationId").toString());
        map.put("id",id);
        List CourseSmallList = courseSmallService.getCourseSmallByClassification(map);

        if(CourseSmallList.size()>0){
            ListPageUtils pageUtil = new ListPageUtils(CourseSmallList,pageSize);
            List list = pageUtil.getPagedList(pageNum);
            if(list.size() > 0){
                return R.ok("获取微课成功",100).put("data",list).put("nextNum",pageNum+1);
            }
            return R.ok("获取微课成功",100).put("data",list).put("nextNum",-1);
        }
        return R.ok("课程空空如也~",100).put("data",CourseSmallList).put("nextNum",-1);
    }

}
